package persistence.javaserialize;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import model.entity.container.ProductContainer;
import model.entity.container.ProductGroup;
import model.entity.container.StorageUnit;
import persistence.ProductContainersDAO;

import common.Persistence;
import common.Result;
import common.ValueResult;

/**
 * Contains methods for retrieving, adding, updating, and deleting product containers from
 * serialized java objects.
 * 
 * @author tdixon
 */
public class SerialProductContainersDAO implements ProductContainersDAO
{
  @SuppressWarnings("unchecked")
  @Override
  public ValueResult<Set<StorageUnit>> getStorageUnits()
  {
    try
    {
      return new ValueResult<Set<StorageUnit>>(true, "",
          (Set<StorageUnit>) Persistence.load("StorageUnits"));
    }
    catch (FileNotFoundException e)
    {
      return new ValueResult<Set<StorageUnit>>(true, "", new HashSet<StorageUnit>());
    }
    catch (Exception e)
    {
      return new ValueResult<Set<StorageUnit>>(false, e.getMessage());
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public ValueResult<List<ProductGroup>> getProductGroups()
  {
    try
    {
      return new ValueResult<List<ProductGroup>>(true, "",
          (List<ProductGroup>) Persistence.load("ProductGroups"));
    }
    catch (FileNotFoundException e)
    {
      return new ValueResult<List<ProductGroup>>(true, "", new ArrayList<ProductGroup>());
    }
    catch (Exception e)
    {
      return new ValueResult<List<ProductGroup>>(false, e.getMessage());
    }
  }

  @Override
  public Result add(ProductContainer dto)
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Result update(ProductContainer dto)
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Result delete(ProductContainer dto)
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Result saveStorageUnits(Set<StorageUnit> storageUnits)
  {
    try
    {
      Persistence.save(storageUnits, "StorageUnits");
      return new Result(true);
    }
    catch (Exception e)
    {
      return new Result(false, e.getMessage());
    }
  }

  @Override
  public Result saveProductGroups(List<ProductGroup> productGroups)
  {
    try
    {
      Persistence.save(productGroups, "ProductGroups");
      return new Result(true);
    }
    catch (Exception e)
    {
      return new Result(false, e.getMessage());
    }
  }

}
